//
//  HXMeetingNotifyDetailsViewController.m
//  KD
//
//  Created by ifly on 16/3/10.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXMeetingNotifyDetailsViewController.h"
#import "HXMeetingNotifyDetailsHeaderView.h"
#import "HXRevertViewController.h"
#import "HXRevertQuestionListViewController.h"
#import "HXReachTableViewCell.h"

@interface HXMeetingNotifyDetailsViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) HXNotifyDetailsModel *data;
@property(nonatomic,weak) HXMeetingNotifyDetailsHeaderView *headerView;
@property(nonatomic,assign) NSInteger index;
@property(nonatomic,strong) NSMutableArray<HXReachListModel *> *dataArray ;

@end

@implementation HXMeetingNotifyDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self.meetingType isEqualToString:@"neibu"]) {
        self.title = @"会议详情";
    }else{
        self.title = @"内部会议详情";
    }
    self.tableView.tableFooterView = [[UIView alloc]init];
    UIButton *backButton=[[UIButton alloc]init];
    // 设置返回图片
    [backButton setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    backButton.contentMode = UIViewContentModeLeft;
    backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    //        [button sizeToFit];//让按钮大小跟随内容
    // 让按钮的内容往左边偏移10
    backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    backButton.frame=CGRectMake(0, 0,37,90);
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:backButton];
}

-(void)back{
    if ([[UserDefaultHandler getUserDetault:kUsers_Kind] integerValue] == UserTypeManage) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    if (!(([self.data.details.MU_IsJoin isEqualToString:@"1"])||([self.data.details.MU_IsJoin isEqualToString:@"0"]))) {//没回执
        CTActionSheet *actionSheet = [CTActionSheet actionSheetWithTitle:nil cancelButtonTitle:nil destructiveButtonTitle:@"本人参会" otherButtonTitles:@"他人代我参会" HandleBlock:^(int btnIndex) {
            if (btnIndex == 0) {
                [NetworkAPI meetingReceiptWithTMID:[self.messageDict objectForKey:@"data.TMUserID"]
                                         MU_IsJoin:@"true"
                                     MU_ReplaceMan:@""
                                  MU_ReplaceManTel:@""
                                           Success:^(HXBaseModel *result) {
                                               if ([result.code isEqualToString:@"200"]) {
                                                   [self.navigationController popViewControllerAnimated:YES];
                                                   [SVProgressHUD showSuccessWithStatus:@"回执成功"];
                                               }else{
                                                   [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                               }
                                           } failure:^(NSError *err) {
                                               [self.navigationController popViewControllerAnimated:YES];
                                               [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                           }];
            }else if (btnIndex == 1){
                UIAlertController *changePunishAlert=[UIAlertController alertControllerWithTitle:@"替换人信息" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                [changePunishAlert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                    textField.placeholder=@"替换人名字";
                }];
                [changePunishAlert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                    textField.placeholder=@"替换人电话";
                }];
                UIAlertAction *cretainButtn=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
                    NSString *name=changePunishAlert.textFields[0].text;
                    NSString *phone=changePunishAlert.textFields[1].text;
                    [NetworkAPI meetingReceiptWithTMID:[self.messageDict objectForKey:@"data.TMUserID"]
                                             MU_IsJoin:@"false"
                                         MU_ReplaceMan:name
                                      MU_ReplaceManTel:phone
                                               Success:^(HXBaseModel *result) {
                                                   if ([result.code isEqualToString:@"200"]) {
                                                       [self.navigationController popViewControllerAnimated:YES];
                                                       [SVProgressHUD showSuccessWithStatus:@"回执成功"];
                                                   }else{
                                                       [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                                   }
                                               } failure:^(NSError *err) {
                                                   [self.navigationController popViewControllerAnimated:YES];
                                                   [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                               }];
                }];
                [changePunishAlert addAction:cretainButtn];
                [self presentViewController:changePunishAlert animated:YES completion:nil];
            }
        }];
        UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
        if ([window.subviews containsObject:self.view]) {
            [actionSheet showInView:self.view];
        } else {
            [actionSheet showInView:window];
        }
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(void)setUpHeaderView:(HXNotifyDetailsModel *)data{
    HXMeetingNotifyDetailsHeaderView *headerView = [[[NSBundle mainBundle]loadNibNamed:@"HXMeetingNotifyDetailsHeaderView" owner:self options:nil]lastObject];
    headerView.data = data;
    headerView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, headerView.height);
    self.tableView.tableHeaderView = headerView;
    self.headerView = headerView;
    self.headerView.messageDict = self.messageDict;
    headerView.meetingType = self.meetingType;
}

- (IBAction)questionOneBtnClick:(id)sender {
    if ([[UserDefaultHandler getUserDetault:kUsers_Kind] integerValue] == UserTypeManage){//是主管
        HXRevertQuestionListViewController *vc = [[HXRevertQuestionListViewController alloc]init];
        vc.quesdata = self.data.questlist[0];
        [self.navigationController pushViewController:vc animated:YES];
    }else if([[UserDefaultHandler getUserDetault:kUsers_Kind] integerValue] == UserTypeCorp){//是企业
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"HXInform" bundle:nil];
        HXRevertViewController *vc = [sb instantiateViewControllerWithIdentifier:@"HXRevertViewController"];
        vc.endDate = self.data.details.EndDate;
        vc.tmUserID = [self.messageDict objectForKey:@"data.TMUserID"];
        if (self.data.questlist.count>0) {
            vc.quesdata = self.data.questlist[0];
        }
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        //NSAssert(0, @"回复问题判断用户类型崩溃");
    }
}
- (IBAction)questionTwoBtnClick:(id)sender {
    if ([[UserDefaultHandler getUserDetault:kUsers_Kind] integerValue] == UserTypeManage){//是主管
        HXRevertQuestionListViewController *vc = [[HXRevertQuestionListViewController alloc]init];
        vc.quesdata = self.data.questlist[0];
        [self.navigationController pushViewController:vc animated:YES];
    }else if([[UserDefaultHandler getUserDetault:kUsers_Kind] integerValue] == UserTypeCorp){//是企业
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"HXInform" bundle:nil];
        HXRevertViewController *vc = [sb instantiateViewControllerWithIdentifier:@"HXRevertViewController"];
        vc.endDate = self.data.details.EndDate;
        vc.tmUserID = [self.messageDict objectForKey:@"data.TMUserID"];
        if (self.data.questlist.count>1) {
            vc.quesdata = self.data.questlist[1];
        }
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        //NSAssert(0, @"回复问题判断用户类型崩溃");
    }
}

- (void)setMessageDict:(NSMutableDictionary *)messageDict{
    _messageDict = messageDict;
     [SVProgressHUD showWithStatus:@"正在获取" ];
    [NetworkAPI notifyDetailsWithMessageId:[_messageDict objectForKey:@"data.MessageID"]
                                  tMUserID:[_messageDict objectForKey:@"data.TMUserID"]
                                  userKind:[UserDefaultHandler getUserDetault:kUsers_Kind]
                                   success:^(HXNotifyDetailsModel *result) {
                                       [SVProgressHUD dismiss];
                                       if ([result.code isEqualToString:@"200"]) {
                                           [SVProgressHUD showSuccessWithStatus:@"获取成功"];
                                           self.data = result;
                                           [self setUpHeaderView:result];
                                       }
                                   } failure:^(NSError *err) {
                                       [SVProgressHUD dismiss];
                                       [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                   }];
    [self.tableView.mj_footer beginRefreshing];
}

#pragma mark - 懒加载
- (NSMutableArray<HXReachListModel *> *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}

@end
